﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace UpdateApp
{
    public static class UpdateTool
    {
        public static int VersionCode = 42;
        public static string VersionPath
        {
            get
            {
                return "http://122.112.226.39:8082/topvision-api/app/sysversion/getNewestVersion";
            }
        }
        // http://122.112.226.39:8082/topvision-api/app/sysversion/getNewestVersion 
        // http://topvision-cv.com/TopVisionWeb/app/sysversion?packageNameSch=com.topvision.videostreambackstage.pc&platformSch=1

        public static string platform = "1";    //0 阿里 //1 华为
        public const string packageName = "com.topvision.remotesupport.meetingvideoplay";
        public static string urlFormat = "{0}?packageName={1}&platform={2}";
        public static AppVersion AppVersionConfig;

        public static int Check()
        {
            string url = string.Format(urlFormat, VersionPath, packageName, platform);

            WebClient client = new WebClient();
            using (Stream st = client.OpenRead(url))
            {
                StreamReader sr = new StreamReader(st);
                string json = sr.ReadToEnd();
                var vr = JsonConvert.DeserializeObject<VersionRoot>(json);
                AppVersionConfig = vr.data;
                if (AppVersionConfig == null)
                {
                    return 0;
                }

                int newCode = int.Parse(AppVersionConfig.versioncode);
                int curCode = VersionCode;
                if (newCode > curCode)
                {
                    if (AppVersionConfig.must == 1)
                        return 2;
                    return 1;
                }
                else
                {
                    return 0;
                }
            }
        }
    }
}
